package org.dkakunsi.aplikasitoko.form.gudang;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.dkakunsi.aplikasitoko.data.entity.Barang;
import org.dkakunsi.aplikasitoko.data.entity.Pelaku;
import org.dkakunsi.aplikasitoko.data.entity.Pembelian;
import org.dkakunsi.aplikasitoko.data.entity.Transaksi;
import org.dkakunsi.aplikasitoko.data.entity.TransaksiBarang;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.data.util.Generator;
import org.dkakunsi.aplikasitoko.form.Desktop;
import org.dkakunsi.aplikasitoko.form.MyForm;
import org.dkakunsi.aplikasitoko.service.DataService;

public class Gudang extends MyForm {

	private static final long serialVersionUID = 1L;

	private JLabel labelIdBarang;
	private JLabel labelNamaBarang;
	private JLabel labelSupplier;
	private JLabel labelJumlahBarang;
	private JTextField textFieldIdBarang;
	private JTextField textFieldNamaBarang;
	private JTextField textFieldSupplier;
	private JTextField textFieldJumlahBarang;
	private JButton btnCari;
	private JButton btnCari2;
	private JButton btnTambah;
	private JButton btnOK;
	private String[] kolom = {"ID Barang", "Nama Barang", "Jumlah"};
	
	private List<Barang> lsLoaded;
	private Barang barang;
	private Pelaku pelaku;
	private Transaksi transaksi;
	private List<Barang> barangInTransaksi;
	private List<TransaksiBarang> tbInTransaksi;
	private int index;

	/**
	 * create frame with no desktop initialized
	 * @param judul
	 */
 	public Gudang(String judul) {
		super(judul);
		setter();
	}

	/**
	 * create frame with desktop initialized
	 * @param judul
	 * @param desktop
	 */
	public Gudang(String judul, Desktop desktop) {
		super(judul, desktop);
		setter();
	}

	private void setter(){
		ds = new DataService();
		this.setUkuran(940, 270);
		this.setUkuranTable(500, 215);
		this.setBtnExit(820, 200);
		this.setTableModel(kolom);
		loadInitialize();
		
		labelIdBarang = new JLabel("ID Barang");
		labelIdBarang.setBounds(525, 12, 150, 25);
		getContentPane().add(labelIdBarang);
		
		textFieldIdBarang = new JTextField();
		textFieldIdBarang.setBounds(690, 12, 120, 25);
		getContentPane().add(textFieldIdBarang);
		
		btnCari = new JButton("CARI");
		btnCari.setBounds(820, 12, 100, 25);
		getContentPane().add(btnCari);
		btnCari.setActionCommand("CARI");
		btnCari.addActionListener(this);

		labelNamaBarang = new JLabel("Nama Barang");
		labelNamaBarang.setBounds(525, 47, 150, 25);
		getContentPane().add(labelNamaBarang);
		
		textFieldNamaBarang = new JTextField();
		textFieldNamaBarang.setBounds(690, 47, 230, 25);
		getContentPane().add(textFieldNamaBarang);
		
		labelSupplier = new JLabel("Supplier");
		labelSupplier.setBounds(525, 82, 120, 25);
		getContentPane().add(labelSupplier);
		
		textFieldSupplier = new JTextField();
		textFieldSupplier.setBounds(690, 82, 120, 25);
		getContentPane().add(textFieldSupplier);

		btnCari2 = new JButton("CARI");
		btnCari2.setBounds(820, 82, 100, 25);
		getContentPane().add(btnCari2);
		btnCari2.setActionCommand("CARI2");
		btnCari2.addActionListener(this);
		
		labelJumlahBarang = new JLabel("Jumlah");
		labelJumlahBarang .setBounds(525, 117, 120, 25);
		getContentPane().add(labelJumlahBarang);
		
		textFieldJumlahBarang = new JTextField();
		textFieldJumlahBarang.setBounds(690, 117, 230, 25);
		getContentPane().add(textFieldJumlahBarang);

		btnTambah = new JButton("TAMBAH");
		btnTambah.setBounds(690, 150, 100, 25);
		getContentPane().add(btnTambah);
		btnTambah.setActionCommand("ADD");
		btnTambah.addActionListener(this);

		btnOK = new JButton("SIMPAN");
		btnOK.setBounds(820, 150, 100, 25);
		getContentPane().add(btnOK);
		btnOK.setActionCommand("OK");
		btnOK.addActionListener(this);
		
		setView();
		index=0;
		barangInTransaksi = new ArrayList<>();
		tbInTransaksi = new ArrayList<>();
	}
	
	private void setView(){
		this.btnCari.setEnabled(true);
		this.btnCari2.setEnabled(true);
		this.btnOK.setEnabled(false);
		
		this.textFieldIdBarang.setEnabled(true);
		this.textFieldNamaBarang.setEnabled(false);
		this.textFieldSupplier.setEnabled(true);
		this.textFieldJumlahBarang.setEnabled(true);
		
		this.textFieldIdBarang.setText("");
		this.textFieldNamaBarang.setText("");
		this.textFieldSupplier.setText("");
		this.textFieldJumlahBarang.setText("");

		this.textFieldIdBarang.requestFocus();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		switch(command){
			case "EXIT" : exit();
				break;
			case "ADD" :
				index++;
				TransaksiBarang tb = new TransaksiBarang();
				tb.setJumlahBarang(Integer.parseInt(this.textFieldJumlahBarang.getText()));
				barang.tambahJumlahBarang(Integer.parseInt(this.textFieldJumlahBarang.getText()));

				barangInTransaksi.add(barang);
				tbInTransaksi.add(tb);

				setView();
				this.btnOK.setEnabled(true);
				this.btnCari2.setEnabled(false);
				break;
			case "CARI" : 
				try {
					barang = (Barang) ds.getInstance(textFieldIdBarang.getText(), "BARANG");
					this.textFieldNamaBarang.setText(barang.getNama());
				} catch (ApplicationDataException e1) {
					JOptionPane.showMessageDialog(this, e1.getMessage(), "Kesalahan Kode", JOptionPane.ERROR_MESSAGE);
					// e1.printStackTrace();
				}
				break;
			case "CARI2" :
				try {
					pelaku = (Pelaku) ds.getInstance(textFieldSupplier.getText(), "PELAKU");
					this.btnCari2.setEnabled(false);
				} catch (ApplicationDataException e1) {
					JOptionPane.showMessageDialog(this, e1.getMessage(), "Kesalahan Kode", JOptionPane.ERROR_MESSAGE);
					// e1.printStackTrace();
				}
				break;
			case "OK" :
				this.btnCari2.setEnabled(true);
				ds.attach(pelaku);
				transaksi = createPembelian();
				transaksi.setJenisTransaksi("Pembelian");
				pelaku.addTransaksi(transaksi);

				double jumlahTransaksi = iterativeOK();
				transaksi.setJumlahTransaksi(jumlahTransaksi);


				try {
					ds.save(transaksi);
//					ds.update(barang);
				} catch (ApplicationDataException e1) {
					JOptionPane.showMessageDialog(this, "Call the Developer", "Application Error", JOptionPane.ERROR_MESSAGE);
					// System.out.println(e1.getMessage());
					//e1.printStackTrace();
				}
				transaksi = null;
				barang = new Barang();
				barang = null;
				setView();
				loadInitialize();
				break;
			default :
				break;
		}
	}

	private Double iterativeOK(){
		double jumlahTransaksi = 0.0;
		for(int i=0; i<index; i++){
			Barang barangFound = (Barang) barangInTransaksi.get(i);
			ds.attach(barangFound);
			TransaksiBarang tbFound = (TransaksiBarang) tbInTransaksi.get(i);
			barangFound.addTransaksiBarang(tbFound);
			transaksi.addTransaksiBarang(tbFound);
//			barang.tambahJumlahBarang(tbFound.getJumlahBarang());
			jumlahTransaksi += tbFound.getBeliJumlahBayar();
		}
		return jumlahTransaksi;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void loadInitialize() {
		try {
			lsLoaded = (List<Barang>) ds.getAll("BARANG");
			loadView();
		} catch (ApplicationDataException e) {
			System.out.println(e.getMessage() + "\n@ DataBarang.load()");
		}
	}

	@Override
	protected void loadView() {
		this.setTableModel(kolom);

		Iterator<Barang> itL = lsLoaded.iterator();
		while(itL.hasNext()){
			Barang barang = itL.next();
			try {
				String[] data = { barang.getKode(), barang.getNama(), barang.getJumlah().toString() };
				tabModel.addRow(data);
			} catch (NullPointerException exc){}
		}
	}

	private Pembelian createPembelian(){
		Pembelian beli = new Pembelian();
		beli.setTanggalPembelian(Generator.getDateNow());
//		beli.setKode(Generator.getKodeTransaksi(beli));
		
		return beli;
	}
}
